//
//  MyJobListTableViewCell.m
//  MobileOffice
//
//  Created by jjkj on 16/11/17.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "MyJobListTableViewCell.h"

@implementation MyJobListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.buttonArr=[NSMutableArray new];
        
        self.lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 1)];
        self.lineLabel.backgroundColor=BACKCOLOR_GRAY;
        [self.contentView addSubview:self.lineLabel];
        
        self.statusLabel=[MyControl creatLabelWithFrame:CGRectMake(8, 8, 60, 60) text:@""];
        self.statusLabel.textAlignment=NSTextAlignmentCenter;
        self.statusLabel.textColor=[UIColor whiteColor];
        self.statusLabel.layer.cornerRadius=8;
        self.statusLabel.layer.masksToBounds=YES;
        self.statusLabel.backgroundColor=COLOR_PINK;
        self.statusLabel.font=[UIFont boldSystemFontOfSize:13];
        [self.contentView addSubview:self.statusLabel];
        
        self.signButton=[[UIButton alloc]initWithFrame:CGRectMake(IPHONE_WIDTH-30, self.statusLabel.y, 30, 20)];
//        [self.signButton setTitle:titleArr[i] forState:UIControlStateNormal];
        [self.signButton setImage:[UIImage imageNamed:@"jiantou_down@2x"] forState:UIControlStateNormal];
        [self.signButton setImage:[UIImage imageNamed:@"jiantou_up@2x"] forState:UIControlStateSelected];
//        button.imageEdgeInsets=UIEdgeInsetsMake(10, button.width-30, 10, 10);
//        button.titleEdgeInsets=UIEdgeInsetsMake(10, 10, 10, 30);
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [button setTitleColor:COLOR_SKYBLUE forState:UIControlStateSelected];
        //button.backgroundColor=[UIColor redColor];
//        button.titleLabel.font=FONT_14;
        //        [button setTitleColor:COLOR_MIDBLUE forState:UIControlStateSelected];
//        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.signButton];
        
        self.timeLabel=[MyControl creatLabelWithFrame:CGRectMake(self.signButton.x-100, self.statusLabel.y, 100, 20) text:@""];
        self.timeLabel.font=FONT_12;
        self.timeLabel.textAlignment=NSTextAlignmentRight;
        [self.contentView addSubview:self.timeLabel];
        
        self.nameLabel=[MyControl creatLabelWithFrame:CGRectMake(self.statusLabel.x+self.statusLabel.width+8, 8, self.timeLabel.x-(self.statusLabel.x+self.statusLabel.width+8), 20) text:@""];
        self.nameLabel.font=FONT_13;
        [self.contentView addSubview:self.nameLabel];
        
        self.holidayLabel=[MyControl creatLabelWithFrame:CGRectZero text:@""];
        self.holidayLabel.font=FONT_11;
        self.holidayLabel.textColor=[UIColor lightGrayColor];
        [self.contentView addSubview:self.holidayLabel];
        
        self.dateTimeLabel=[MyControl creatLabelWithFrame:CGRectMake(self.nameLabel.x, self.nameLabel.y+self.nameLabel.height, IPHONE_WIDTH-(self.statusLabel.x+self.statusLabel.width+8)-8, 20) text:@""];
        self.dateTimeLabel.textColor=[UIColor lightGrayColor];
        self.dateTimeLabel.font=FONT_12;
        [self.contentView addSubview:self.dateTimeLabel];
        
        self.reasonLabel=[MyControl creatLabelWithFrame:CGRectMake(self.nameLabel.x, self.dateTimeLabel.y+self.dateTimeLabel.height, self.dateTimeLabel.width, 20) text:@""];
        self.reasonLabel.font=FONT_12;
        self.reasonLabel.textColor=[UIColor lightGrayColor];
        [self.contentView addSubview:self.reasonLabel];
        
        self.backScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, self.statusLabel.y+self.statusLabel.height+8, IPHONE_WIDTH, 0)];
        self.backScrollView.bounces=NO;
        self.backScrollView.showsVerticalScrollIndicator=NO;
        self.backScrollView.showsHorizontalScrollIndicator=NO;
        //self.backScrollView.backgroundColor=[UIColor blueColor];
        [self.contentView addSubview:self.backScrollView];
        
        self.signImageView=[UIImageView new];
        self.signImageView.image=[UIImage imageNamed:@"k_jantou_reds@2x"];
        [self.backScrollView addSubview:self.signImageView];
        
        self.scrollViewLine=[[UILabel alloc]initWithFrame:CGRectZero];
        self.scrollViewLine.backgroundColor=[UIColor grayColor];
        [self.backScrollView addSubview:self.scrollViewLine];
    }
    return self;
}

//设置流程轴
- (void)setScrollViewWithModel:(MyJobListModel *)model{
    if (model.isOpen) {
        self.backScrollView.height=80;
    }else{
        self.backScrollView.height=0;
    }
    self.backScrollView.y=self.reasonLabel.y+self.reasonLabel.height+8;
    NSMutableArray *dataArr=model.subDataArr;
    //设置流程线的frame
    self.scrollViewLine.frame=CGRectMake(40, 5, 80*(dataArr.count-1), 0.5);
    for (int i=0; i<dataArr.count; i++) {
        UIButton *titleButton=nil;
        MyJobListModel *model=dataArr[i];
        //这里的titleButton要重复利用
        if (i<self.buttonArr.count) {
            titleButton=self.buttonArr[i];
        }else{
            titleButton=[UIButton buttonWithType:UIButtonTypeCustom];
            [self.buttonArr addObject:titleButton];
        }
        titleButton.hidden=NO;
        //titleButton.backgroundColor=[UIColor greenColor];
        titleButton.frame=CGRectMake(30+80*i, 0, 30, 45);
        [titleButton setTitle:model.itemValue forState:UIControlStateNormal];
        titleButton.titleLabel.font=[UIFont boldSystemFontOfSize:13];
        titleButton.titleLabel.numberOfLines=0;
        titleButton.titleLabel.textAlignment=NSTextAlignmentCenter;
        //titleButton.titleLabel.backgroundColor=[UIColor purpleColor];
        [titleButton setTitleColor:COLOR_PINK forState:UIControlStateSelected];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置图片
        [titleButton setImage:[UIImage imageNamed:@"k_dian_1@2x"] forState:UIControlStateNormal];
        [titleButton setImage:[UIImage imageNamed:@"k_dian_2@2x"] forState:UIControlStateSelected];
        //设置图片的位置
        titleButton.imageEdgeInsets=UIEdgeInsetsMake(0, 10, 35, 10);
        titleButton.titleEdgeInsets=UIEdgeInsetsMake(10, -18, 0, 0);
        //如果是当前所处的流程则标为红色
        if ([model.itemState isEqualToString:@"2"]) {
            titleButton.selected=YES;
            self.signImageView.frame=CGRectMake((titleButton.width-10)/2+titleButton.x, titleButton.y+titleButton.height+3, 10, 10);
        }else{
            titleButton.selected=NO;
        }
        [self.backScrollView addSubview:titleButton];
    }
    //因为cell会复用 所以要把不用的label隐藏
    if (self.buttonArr.count>dataArr.count) {
        for (NSInteger i=dataArr.count; i<self.buttonArr.count; i++) {
            UIButton *titleButton=self.buttonArr[i];
            titleButton.hidden=YES;
        }
    }
    _backScrollView.contentSize=CGSizeMake(dataArr.count*80+2, 0);
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
